package com.itheima.reggie.controller;

import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlShowColumnOutpuVisitor;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    //新增地址
    @PostMapping
    public R<String> saveAddress(@RequestBody AddressBook addressBook){
        //获取id
        Long id = BaseContext.getCurrentId();
        addressBook.setUserId(id);
        boolean save = addressBookService.save(addressBook);
        return save?R.success("添加成功"):R.error("添加失败");
    }

    //设置默认地址
    @PutMapping("/default")
    public R<AddressBook> defaultAddress(@RequestBody AddressBook addressBook){
        LambdaUpdateWrapper<AddressBook> lqw = new LambdaUpdateWrapper<>();
        // 先将默认设置为0
        lqw.set(AddressBook::getIsDefault,0);
        lqw.eq(AddressBook::getUserId, BaseContext.getCurrentId());// where user_id =?
        addressBookService.update(lqw);
        //将默认地址状态设为1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    //根据ID查询地址
    @GetMapping("/{id}")
    public R selectAddress(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null){
            return R.success(addressBook);
        }else {

            return R.error("未查询到地址信息");
        }
    }

    //查询默认地址
    @GetMapping("/default")
    public R selectDefaultAddress(){
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        lqw.eq(AddressBook::getIsDefault, 1);

        AddressBook addressBook = addressBookService.getOne(lqw);
        //判断是否为空
        if (addressBook != null){
            return R.success(addressBook);
        }
        return R.error("查询地址信息有误");
    }

    //查询指定用户的全部地址
    @GetMapping("/list")
    public R selectAllAddress(AddressBook addressBook){
        //查询id
        addressBook.setUserId(BaseContext.getCurrentId());

        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        return R.success(addressBookService.list(queryWrapper));
    }
    //删除地址
    @DeleteMapping
    public R deleteAddress(@RequestParam List<Long> ids){
        boolean remove = addressBookService.removeByIds(ids);
        return remove?R.success("删除成功"):R.error("删除失败");
    }

    //修改地址
    @PutMapping
    public R updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }
}
